﻿#if UNITY_EDITOR
using UnityEditor;
#endif
using UnityEngine;

[ExecuteInEditMode]
public class SizeBySize : MonoBehaviour
{
    public bool heightByWidth;
    public float heightByWidthRatio;
    public bool widthByHeight;
    public float widthByHeightRatio;

    private RectTransform rt;

    private void OnEnable()
    {
        rt = GetComponent<RectTransform>();
#if UNITY_EDITOR
        EditorApplication.update += Update;
#endif
    }

    private void Update()
    {
        if (heightByWidth)
        {
            rt.sizeDelta = new Vector2(rt.sizeDelta.x, rt.sizeDelta.x * heightByWidthRatio);
        }

        if (widthByHeight)
        {
            rt.sizeDelta = new Vector2(rt.sizeDelta.y * widthByHeightRatio, rt.sizeDelta.y);
        }
    }

    private void OnDisable()
    {
#if UNITY_EDITOR
        EditorApplication.update -= Update;
#endif
    }
}
